package com.rich.house.service.impl;

import com.rich.common.constant.HouseAuditConstant;
import com.rich.common.constant.HouseAuditString;
import com.rich.common.constant.MyToken;
import com.rich.common.dto.house.Result;
import com.rich.common.exception.CustomException;
import com.rich.common.exception.ErrorEnum;
import com.rich.house.mapper.AuditMapper;
import com.rich.house.mapper.HouseMapper;
import com.rich.house.mapper.WantMapper;
import com.rich.house.service.AuditService;
import com.rich.house.vo.AuditVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @Author: guofeng
 * @Date: 2021/08/16/21:40
 * @Description:
 */
@Service
@Slf4j
public class AuditServiceImpl implements AuditService {

    @Autowired
    private AuditMapper auditMapper;

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private WantMapper wantMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public List<AuditVo> findAll(AuditVo auditVo) {
        return auditMapper.findAll(auditVo);
    }

    @Override
    public void update(AuditVo auditVo) {
        log.info("进入延时队列");
        rabbitTemplate.convertAndSend("house-audit-direct-exchange","house-audit-ttl",auditVo);
    }

    @Override
    public Result createDeleteToken() {
        String token = UUID.randomUUID().toString().replace("-", "");
        redisTemplate.opsForValue().set( MyToken.HouseAuditDeleteToken,token);
        return new Result(token);
    }

    @Override
    public Result delete(AuditVo auditVo, String deleteToken) {
        String script ="if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end";
        Long execute = redisTemplate.execute(new DefaultRedisScript<>(script, Long.class), Arrays.asList(MyToken.HouseAuditDeleteToken),deleteToken );
        if(execute != 0) {
            if (HouseAuditConstant.BUY_HOUSE.equals(auditVo.getType()) || HouseAuditConstant.RENT_HOUSE.equals(auditVo.getType())) {
                houseMapper.delete(auditVo);
            } else {
                wantMapper.delete(auditVo);
            }
            return new Result(HouseAuditString.OK);
        }
        throw new CustomException(ErrorEnum.HOUSE_AUDIT_DATA_DELETE_ERROR);
    }

    @Override
    public AuditVo findOne(AuditVo auditVo) {
        System.out.println(auditVo);
        if(HouseAuditConstant.BUY_HOUSE.equals(auditVo.getType()) || HouseAuditConstant.RENT_HOUSE.equals(auditVo.getType())){
            return houseMapper.findOne(auditVo);
        }else{
            return wantMapper.findOne(auditVo);
        }
    }
}
